package com.data.security;

import java.util.UUID;


import com.data.security.modelo.ControlPin;
import com.data.security.modelo.Preferencias;
import com.data.security.modelo.sebastian.ShaCrypt;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class NuevoUserActivity extends Activity {

	private EditText usuario, uno, dos, tres, cuatro;
	private ImageView adver;
	private ControlPin pin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actividad_nuevouser);
		cargarViews();

		pin = new ControlPin(this, uno, dos, tres, cuatro);

	}

	public void registrarse(View view) {
		AlertDialog.Builder bbuil=new AlertDialog.Builder(this);
		bbuil.setTitle(getString(R.string.app_name));
		bbuil.setMessage(getString(R.string.mensajeinicio));
		bbuil.setPositiveButton("Ok", listener);
		bbuil.create().show();
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			adver.setVisibility(View.INVISIBLE);
			String usu = usuario.getText().toString();
			String pass = pin.getText();

			if (!usu.trim().equals("")) {
				if (!pass.trim().equals("") && pass.trim().length() == 4) {
					Preferencias.setLlave(getApplicationContext(), usu + pass
							+ UUID.randomUUID().toString());

					byte[] uss = ShaCrypt.generarArreglo(usu.getBytes(),
							ShaCrypt.SHA256);
					usu = Base64.encodeToString(uss, 0, uss.length,
							Base64.NO_WRAP);
					// Log.d("usuario", usu);
					byte[] clave = ShaCrypt.generarArreglo(pass.getBytes(),
							ShaCrypt.SHA256);
					pass = Base64.encodeToString(clave, 0, clave.length,
							Base64.NO_WRAP);
					// Log.d("pass", pass);
					Preferencias.setUsuario(getApplicationContext(), usu);
					Preferencias.setClave(getApplicationContext(), pass);
					startActivity(new Intent(getApplicationContext(),
							LoginActivity.class));

					NuevoUserActivity.this.finish();
				} else {
					Toast.makeText(getApplicationContext(),
							getString(R.string.passvacio), Toast.LENGTH_LONG)
							.show();
					adver.setVisibility(View.VISIBLE);
				}
			} else {
				Toast.makeText(getApplicationContext(),
						getString(R.string.usuariovacio), Toast.LENGTH_LONG)
						.show();
				adver.setVisibility(View.VISIBLE);
			}

		}

	};

	private void cargarViews() {
		usuario = (EditText) findViewById(R.id.campo_usuario);
		uno = (EditText) findViewById(R.id.campo_password1);
		dos = (EditText) findViewById(R.id.campo_password2);
		tres = (EditText) findViewById(R.id.campo_password3);
		cuatro = (EditText) findViewById(R.id.campo_password4);
		adver = (ImageView) findViewById(R.id.advertenciaimage);
		adver.setVisibility(View.INVISIBLE);
	}

}
